<?php
namespace jy_merch;
defined('BY_JYA') or exit('error');
/*
	分销插件会员类
	如果将分销商各项数据在每次数据变动时计算好并存入数据库。
	那么分别需要做
		在设置上下级关系时，修改新上级的 下线数量、分销商数量、订单数量、订单佣金数量等数据
		在变更上下级关系时，修改原、新上级的各项参数
		在发生订单时，修改上级的订单相关参数
		在用户成为分销商时，修改上级的分销商数量相关数据
*/
class zhaoshang extends \plugin{
	public function checkmember($mid=0,$isexi=false,$isset=false){
			$mid=$mid?:$_SESSION['uid'];
			$member=pdo_get('jy_merch_zhaoshanglist',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));			
			$page_setting=$this->plugin_setting('page_setting');
			$page_setting['tagent']=$page_setting['tagent']?:'代理商';
			if(!$member['mid']){
				$this->register(array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
				$member=pdo_get('jy_merch_zhaoshanglist',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			}
			if(!$member['status']){
				//如果成为分销商无条件，此处自动申请成为分销商
				if($this->setting['become']==3){
					//分销商无需审核，此处自动审核
					$ispass=true;
					$this->apply_agent($member['mid']);
					if($this->setting['become_check'] && $isexi){
						exi('申请正在审核请耐心等待！','error','jy_merch/zhaoshang.apply/');
					}elseif($isset){
						return true;
					}
				}elseif($this->setting['become']==4){
					//消费金额
					$buyfee=pdo_getsum('jy_weishop_order',array('ispay'=>1,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'price');
					if($buyfee>=$this->setting['money']){
						$ispass=true;
						$this->apply_agent($member['mid']);
						if($this->setting['become_check'] && $isexi){
							exi('申请正在审核请耐心等待。','error','jy_merch/zhaoshang.apply/');
						}
					}elseif($isexi){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_merch/zhaoshang.apply/');
					}
				}elseif($this->setting['become']==5){
					//购买指定商品
					$become_goods=$this->setting['become_goods'];
					foreach($become_goods as &$bgv){
						$isbuy=pdo_count('jy_weishop_order_goods',array('mid'=>$member['mid'],'goodsid'=>$bgv['id'],'ispay'=>1),'id');
						if($isbuy>0){
							$ispass=true;
							$this->apply_agent($member['mid']);
							break;
						}
					}
					if($this->setting['become_check'] && $isexi && $ispass){
						exi('您的申请，正在审核请耐心等待！','error','jy_merch/zhaoshang.apply/');
					}elseif($isexi && !$ispass){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_merch/zhaoshang.apply/');
					}
				}else{
					if($isexi){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_merch/zhaoshang.apply/');
					}
				}
				if($ispass && $isexi){
					exi('','','jy_merch/zhaoshang.index/');
				}
			}elseif($member['stop'] && $isexi){
				if($isset){
						return true;
				}
				exi('你的'.$page_setting['tagent'].'身份已禁用！','error','jy_merch/zhaoshang.apply/');
			}elseif((!$member['realname'] || !$member['mobile']) && $isexi){
				if($isset){
					return true;
				}
				if($this->setting['become_reg']==1){
					exi('请先完善信息','error','jy_merch/zhaoshang.info/');
				}else{
					
				}
			}
			return $member;
	}
	/*
		会员注册
	*/
	public function register($member=array()){
		if(!$member['uniacid']){
			$member['uniacid']=$_SESSION['uniacid'];
		}
		if(!$member['mid']){
			$member['mid']=$_SESSION['uid'];
		}
		if(!$member['createtime']){
			$member['createtime']=TIMESTAMP;
		}
		if(!$member['mid']){
			return error('招商员注册失败，缺少会员ID！');
		}
		$ismid=$this->get('zhaoshanglist',array('mid'=>$member['mid']),array('mid'));
		if($ismid){
			return false;
		}
		if($this->zhaoshang['isopen']!=1){
			return false;
		}
		$this->insert('zhaoshanglist',$member);
	}
	/*
		申请成为分销商
	*/
	public function apply_agent($mid,$noapply=false){
		$member=pdo_get('jy_merch_zhaoshanglist',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		if(!$member['status'] && !$this->setting['become_check']){
			//分销商无需审核，此处自动审核
			$this->check_agent($member['id'],1);
		}
		return true;
	}
	/*
		审核区域代理
	*/
	public function check_agent($agent_id,$status,$remark=''){
		if(!$agent_id || !$status){
			return;
		}
		$agent_area=$this->get('zhaoshanglist',array('id'=>$agent_id));
		if(!$agent_area){
			return;
		}
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
		);
		if($update['status']==1){
		}else{
			$update['remark']=$remark;
		}
		$this->update('zhaoshanglist',$update,array('id'=>$agent_id));
		return true;
	}
	/*
		禁用代理商
	*/
	public function stop_agent($agent_id){
		$this->update('zhaoshanglist',array('status'=>2),array('mid'=>$agent_id));
	}
	/*
		恢复代理商
	*/
	public function start_agent($agent_id){
		$this->update('zhaoshanglist',array('status'=>1),array('mid'=>$agent_id));
	}

	/*
		升级分销商等级
		先检测会员身份是否为正常分销商
		然后获取分销商当前等级的下一等级升级条件
		然后逐个条件对比，看是否满足升级条件
		如果全部满足，则升级到相应等级
		
		何时触发升级分销商等级检测呢？
		
	*/
	public function update_agent_level($zhaoshangid){
		$member=$this->get('zhaoshanglist',array('id'=>$zhaoshangid));
		if($member['stop']){
			return;
		}
		$level=pdo_getcolumn('jy_merch_level',array('id'=>$member['level_id']),'level');
		$parameter=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid'],'level >'=>$level),
			'orderby'=>'level desc',
		);
		$levels=$this->getall('level',$parameter['params'],'','',$parameter['orderby']);
		if(empty($levels)){
			return;
		}
		$level_up_types=$this->model()->current_level_up_types();//获取当前设置的分销商等级升级条件
		if(!$level_up_types){
			return;
		}
		foreach($levels as $lv){
			$conditions=json_decode($lv['conditions'],true);
			if(!$level_up_types || !$lv['id']){
				break;
			}
			$up=true;
			foreach($level_up_types as $type){
				if(!$this->level_up_types[$type]){
					continue;
				}
				if($type=='buy_goods' && $lv['goods_ids']){
						$goods_ids=json_decode($lv['goods_ids'],true);
						foreach($goods_ids as $gv){
								if($type=='buy_goods'){
									$isbuynum=pdo_getsum('jy_weishop_order_goods',array('ispay'=>1,'mid'=>$member['mid'],'goodsid'=>$gv['id']),'total');
									if($isbuynum>0){
										break;
									}
								}
						}
						if(!$isbuynum){
							$up=false;
							break;
						}
				}else{
					if($member[$type]<$conditions[$type]){
						$up=false;
						break;
					}
				}
			}
			if($up){
				$next_level=$lv;
				break;
			}
		}
		if($up){
			$update=array(
				'level_id'=>$next_level['id'],
			);
			$this->update('zhaoshanglist',$update,array('id'=>$zhaoshangid));
		}
		return true;
	}
	/*
		邀请奖励
	*/
	public function upyao_fee($merchid=0){
		$user=pdo_get('jy_merch_user',array('id'=>$merchid));
		$zhaoshangid=$user['zhaoshangid'];
		if(!$zhaoshangid){
			return false;
		}
		$zhaoshang=pdo_get('jy_merch_zhaoshanglist',array('id'=>$zhaoshangid));
		$mid=$zhaoshang['mid'];
		$mid=$mid?:$_SESSION['uid'];
		$set=$this->plugin_setting('zhaoshang');
		if($set['yao_open'] && $mid){
			foreach($set['yao_set'] as $k=>$yv){
				if($zhaoshang['level_id']!=$yv['level_id']){
					continue;
				}
				//$num=$this->pdo_count('user',array('zhaoshangid'=>$zhaoshangid,'is_yajinfee >'=>0,'uniacid'=>$_SESSION['uniacid']));
				if($yv['fee']>0){
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$mid,
						'zhaoshangid'=>$zhaoshangid,
						'area_code'=>$zhaoshang['code'],
						'level'=>$zhaoshang['level_id'],
						'money'=>$yv['fee'],
						'commission_type'=>$commissioninfo['commissiontype']?:$set['commissiontype'],
						'createtime'=>TIMESTAMP,
						'yao_key'=>$k,
						'type'=>1,
						'status'=>1,
					);
					$this->insert('commission',$new_commission);
				}
			}
		}
		return false;
	}
}